package com.xh;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import lombok.Data;

import java.security.Security;
import java.util.Properties;

@Configuration
@ConfigurationProperties(prefix = "spring.mail")
@Data
public class EmailConfig {

    private String host;
    private Integer port;
    private String username;
    private String password;

    @Bean
    public JavaMailSenderImpl javaMailSender() {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        final Properties p = System.getProperties() ;
//		p.setProperty("mail.smtp.host", "smtp.qq.com");
        p.setProperty("mail.smtp.auth", "true");
//		p.setProperty("mail.smtp.user", "40087783@qq.com");
//		p.setProperty("mail.smtp.pass", "yqcwbssscqkrbjeh");
        p.setProperty("mail.mime.splitlongparameters", "false");
        p.setProperty("mail.mime.charset", "UTF-8");
        p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        //邮箱发送服务器端口,这里设置为465端口
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setJavaMailProperties(p);
        javaMailSender.setHost(host);
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
        return javaMailSender;
    }
}